﻿using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;

namespace EasyXaf.EntityDesigner.Tools.Extensions;

public static class TypeInfoExtensions
{
    public static Type GetBaseType(this ITypeInfo typeInfo)
    {
        return typeInfo.Type.BaseType;
    }

    public static string GetName(this ITypeInfo typeInfo)
    {
        return typeInfo.Name;
    }

    public static string GetNamespace(this ITypeInfo typeInfo)
    {
        return typeInfo.Type.Namespace;
    }

    public static string GetCaptionFormat(this ITypeInfo typeInfo)
    {
        return typeInfo.FindAttribute<ObjectCaptionFormatAttribute>(false)?.FormatString;
    }

    public static string GetCategory(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<NavigationItemAttribute>(false);
        if (attribute != null)
        {
            return attribute.GroupName == NavigationItemAttribute.DefaultGroupName ? null : attribute.GroupName;
        }
        return null;
    }

    public static bool GetIsAbstract(this ITypeInfo typeInfo)
    {
        return typeInfo.Type.IsAbstract;
    }

    public static string GetTableName(this ITypeInfo typeInfo)
    {
        return typeInfo.FindAttribute<PersistentAttribute>(false)?.MapTo;
    }

    public static string GetDefaultProperty(this ITypeInfo typeInfo)
    {
        if (typeInfo.FindAttribute<System.ComponentModel.DefaultPropertyAttribute>(false) != null || typeInfo.FindAttribute<XafDefaultPropertyAttribute>(false) != null)
        {
            return typeInfo.DefaultMember?.Name;
        }
        return null;
    }

    public static bool? GetIsCloneable(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttributes<ModelDefaultAttribute>(false).FirstOrDefault(a => a.PropertyValue == "IsCloneable");
        if (attribute != null)
        {
            if (bool.TryParse(attribute.PropertyValue, out var isCloneable))
            {
                return isCloneable;
            }
        }
        return null;
    }

    public static bool? GetIsCreatable(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<CreatableItemAttribute>(false);
        if (attribute != null)
        {
            return attribute.IsCreatableItem;
        }
        return null;
    }

    public static bool? GetIsVisibleInNavigations(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<NavigationItemAttribute>(false);
        if (attribute != null)
        {
            return attribute.IsNavigationItem;
        }
        return null;
    }

    public static bool? GetIsVisibleInReports(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<VisibleInReportsAttribute>(false);
        if (attribute != null)
        {
            return attribute.IsVisible;
        }
        return null;
    }

    public static bool? GetIsVisibleInDashboards(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<VisibleInDashboardsAttribute>(false);
        if (attribute != null)
        {
            return attribute.IsVisible;
        }
        return null;
    }

    public static bool? GetIsDeferredDeletion(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<DeferredDeletionAttribute>(false);
        if (attribute != null)
        {
            return attribute.Enabled;
        }
        return null;
    }

    public static bool? GetIsOptimisticLocking(this ITypeInfo typeInfo)
    {
        var attribute = typeInfo.FindAttribute<OptimisticLockingAttribute>(false);
        if (attribute != null)
        {
            return attribute.Enabled;
        }
        return null;
    }
}
